<template>
	<view class="fui-wrap">
		<fui-result v-if="err" type="fail" title="操作失败" :descr="err">
			<fui-button @click="$reLaunch('parking/menus')" width="400rpx" height="84rpx" text="返回菜单" type="success" bold :margin="['48rpx','0','24rpx']"></fui-button>
		</fui-result>
		<fui-form v-if="detail" error-position="1" labelColor="#7a7a7a" ref="form" top="0" :model="formData">
			<fui-form-item label="商户">
				<fui-input readonly :borderBottom="false" :padding="[0]" :value="detail.merch_name"></fui-input>
			</fui-form-item>
			<fui-form-item label="当前余额">
				<merch-balance :size="32" :balance="detail.balance" :settle_type="detail.settle_type"></merch-balance>
			</fui-form-item>
			<fui-form-item label="充值金额" v-if="detail.settle_type=='before' || detail.settle_type=='after'">
				<fui-input type="digit" :borderBottom="false" :padding="[0]" v-model="formData.money">
					<text style="font-size: 24rpx;border-radius: 4rpx;background-color: #8b8b8b;color: #fff;padding: 4rpx 8rpx;">元</text>
				</fui-input>
			</fui-form-item>
			<fui-form-item label="充值时间" v-if="detail.settle_type=='time'">
				<fui-input type="digit" :borderBottom="false" :padding="[0]" v-model="formData.time" @blur="changeTime">
					<text style="font-size: 24rpx;border-radius: 4rpx;background-color: #8b8b8b;color: #fff;padding: 4rpx 8rpx;">分钟</text>
				</fui-input>
			</fui-form-item>
			<fui-form-item label="合计金额" v-if="detail.settle_type=='time' && formData.change_type=='add'">
				<fui-input readonly type="digit" :borderBottom="false" :padding="[0]" v-model="formData.money">
					<text style="font-size: 24rpx;border-radius: 4rpx;background-color: #8b8b8b;color: #fff;padding: 4rpx 8rpx;">元</text>
				</fui-input>
			</fui-form-item>
			<fui-form-item label="充值方式" prop="status">
				<fui-radio-group name="radio" v-model="formData.change_type">
					<view class="fui-list__item">
						<fui-label>
							<view class="fui-align__center">
								<fui-radio scaleRatio="0.8" borderRadius="6rpx" value="add" checked></fui-radio>
								<text style="margin-left: 10rpx;" class="fui-text">增加</text>
							</view>
						</fui-label>
						<fui-label :margin="['0','0','0','0']">
							<view class="fui-align__center">
								<fui-radio scaleRatio="0.8" borderRadius="6rpx" value="minus"></fui-radio>
								<text style="margin-left: 10rpx;" class="fui-text">减少</text>
							</view>
						</fui-label>
						<fui-label :margin="['0','0','0','0']">
							<view class="fui-align__center">
								<fui-radio scaleRatio="0.8" borderRadius="6rpx" value="last"></fui-radio>
								<text style="margin-left: 10rpx;" class="fui-text">最终</text>
							</view>
						</fui-label>
					</view>
				</fui-radio-group>
			</fui-form-item>	
			<fui-form-item label="备注" prop="remark">
				<fui-input :borderBottom="false" :padding="[0]" placeholder="请输入备注" v-model="formData.remark"></fui-input>
			</fui-form-item>
			<view class="fui-btn__box">
				<fui-button :disabled="disabledButton" text="提交" bold @click="submit"></fui-button>
			</view>
		</fui-form>
	</view>
</template>

<script>
	import {methods} from '@/utils/core.js';
	import {formatDuration,parseNumber} from '@/utils/util.js';
	import merchBalance from '@/components/common/merch-balance.vue';
	export default {
		components:{
			merchBalance:merchBalance
		},
		data() {
			return {
				err:'',
				detail:'',
				disabledButton:false,
				formData:{
					merch_id:'',
					time:60,
					money:100,
					remark:'',
					change_type:'add'
				}
			}
		},
		onLoad(e) {
			this.$get('parking/merch/recharge-detail',{merch_id:e.merch_id},true).then(res=>{
				this.detail=res;
				this.formData.merch_id=res.id;
				this.formData.change_type='add';
				if(this.detail.settle_type=='time'){
					this.changeTime();
				}
			}).catch(err=>{
				this.err=err.msg;
			});
		},
		methods: {
			...methods,
			formatDuration,
			changeTime:function(){
				this.formData.money=parseNumber(this.formData.time*this.detail.price/60);
			},
			submit:function(){
				this.disabledButton=true;
				this.$post('parking/merch/recharge',{merch_id:this.detail.id,...this.formData},true,true).then(res=>{
					uni.setStorageSync('refresh',1);
					this.$navigateBack(1500);
				}).catch(err=>{
					this.disabledButton=false;
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.fui-btn__box{
	padding: 20rpx;
}
.fui-list__item{
	display: flex;
	justify-content: space-between;
	width: 80%;
}
.line-between{
	display: flex;
	justify-content: space-between;
	align-items: center;
	text{
		font-size: 24rpx;
		border-radius: 4rpx;
		background-color: #8b8b8b;
		color: #fff;
		padding: 4rpx 8rpx;
	}
}
</style>